package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.AcctPassword;
import com.unionpay.upyzt.resp.AcctPasswordResp;

import java.util.HashMap;
import java.util.Map;

public class AcctPasswordExample {

    /**
     * 电子账簿ID
     */
    private final String balanceAcctId;

    public AcctPasswordExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    /**
     * 运行 Demos
     *
     * @param balanceAcctId 电子账簿ID
     */
    public static void runDemos(String balanceAcctId) throws UpyztException {
        AcctPasswordExample acctPasswordExample = new AcctPasswordExample(balanceAcctId);

        // 获取用于重置交易授权码的短信验证码
        AcctPasswordResp acctPasswordStoreResp = acctPasswordExample.smsCode();

        // 重置交易授权码
        acctPasswordStoreResp = acctPasswordExample.reset();

        // 修改交易授权码
        acctPasswordStoreResp = acctPasswordExample.update();
    }

    /**
     * 修改交易授权码
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AcctPasswordResp update() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("balance_acct_id", balanceAcctId);
        params.put("original_password", Upyzt.encryptField("pa55w0rD"));
        params.put("new_password", Upyzt.encryptField("pa55w0rd"));

        System.out.println("------- 修改交易授权码 -------");
        AcctPasswordResp resp = AcctPassword.update(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 获取用于重置交易授权码的短信验证码
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AcctPasswordResp smsCode() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("balance_acct_id", balanceAcctId);

        System.out.println("------- 获取用于重置交易授权码的短信验证码 -------");
        AcctPasswordResp resp = AcctPassword.smsCode(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 重置交易授权码
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public AcctPasswordResp reset() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("balance_acct_id", balanceAcctId);
        params.put("sms_code", "999999");
        params.put("new_password", Upyzt.encryptField("pa55w0rD"));

        System.out.println("------- 重置交易授权码 -------");
        AcctPasswordResp resp = AcctPassword.reset(params);
        System.out.println(resp);
        return resp;
    }

}
